package com.linkoog.devtools.dubbo.toolwindow.environment;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import com.linkoog.devtools.dubbo.ProtocolEnum;
import com.linkoog.devtools.utils.StringUtils;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

public class DubboEnvironmentAddDialog extends DialogWrapper {

    private JBTextField nameField;
    private JComboBox<String> protocolComboBox;

    private JBTextField ipField;
    private JBTextField portField;
    private JBTextField versionField;
    private JBTextField groupField;

    private JPanel panel;

    @Setter
    private Consumer<DubboEnvironment> callback;

    public DubboEnvironmentAddDialog(DubboEnvironment dubboEnvironment) {
        super((Project) null, false);
        initPanel();
        reset(dubboEnvironment);
        init();
    }

    private void initPanel(){
        this.protocolComboBox = new ComboBox<>();
        ProtocolEnum[] values = ProtocolEnum.values();
        for (ProtocolEnum value : values) {
            this.protocolComboBox.addItem(value.name());
        }

        this.protocolComboBox.setSelectedItem(ProtocolEnum.dubbo.name());
        this.nameField = new JBTextField();
        this.ipField = new JBTextField();
        this.portField = new JBTextField();
        this.versionField = new JBTextField();
        this.groupField = new JBTextField();

        this.panel = FormBuilder.createFormBuilder()
                .addLabeledComponent("Name", nameField)
                .addLabeledComponent("Protocol", protocolComboBox)
                .addLabeledComponent("IP", ipField)
                .addLabeledComponent("Port", portField)
                .addLabeledComponent("Version", versionField)
                .addLabeledComponent("Group", groupField)
                .getPanel();

        panel.setMinimumSize(new Dimension(400, -1));
    }

    private void reset(DubboEnvironment dubboEnvironment){
        if (dubboEnvironment != null){
            if (StringUtils.isNotBlank(dubboEnvironment.getProtocol())){
                this.protocolComboBox.setSelectedItem(dubboEnvironment.getProtocol());
            }

            if (StringUtils.isNotBlank(dubboEnvironment.getName())){
                this.nameField.setText(dubboEnvironment.getName());
            }

            if (StringUtils.isNotBlank(dubboEnvironment.getIp())){
                this.ipField.setText(dubboEnvironment.getIp());
            }

            if (dubboEnvironment.getPort() != null){
                this.portField.setText(dubboEnvironment.getPort().toString());
            }

            if (StringUtils.isNotBlank(dubboEnvironment.getVersion())){
                this.versionField.setText(dubboEnvironment.getVersion());
            }

            if (StringUtils.isNotBlank(dubboEnvironment.getGroup())){
                this.groupField.setText(dubboEnvironment.getGroup());
            }
        }
    }



    @Override
    protected ValidationInfo doValidate() {
        String name = this.nameField.getText();
        if (StringUtils.isBlank(name)){
            return new ValidationInfo("name不能为空", nameField);
        }

        final String portFieldText = this.portField.getText();
        if (StringUtils.isNotBlank(portFieldText)){
            try {
                int portInt =  Integer.parseInt(portFieldText);
                if (!(portInt >= 1 && portInt <= 65535)){
                    return new ValidationInfo("无效的端口，需要在1~65535之间", portField);
                }
            }catch (Exception ignore){
                return new ValidationInfo("错误的端口", portField);
            }
        }

        return null;
    }

    @Override
    protected void doOKAction() {
        DubboEnvironment dubboEnvironment = getDubboEnvironment();
        if (callback != null){
            callback.accept(dubboEnvironment);
        }
        super.doOKAction();
    }



    private DubboEnvironment getDubboEnvironment() {
        String name = this.nameField.getText();
        String protocol = (String) this.protocolComboBox.getSelectedItem();
        String ip = this.ipField.getText();
        Integer port = parsePort(this.portField.getText());
        String version = this.versionField.getText();
        String group = this.groupField.getText();


        DubboEnvironment dubboEnvironment = new DubboEnvironment();
        dubboEnvironment.setName(name);
        dubboEnvironment.setProtocol(protocol);
        dubboEnvironment.setIp(StringUtils.isNotBlank(ip)? ip : "127.0.0.1");
        dubboEnvironment.setPort(port);
        dubboEnvironment.setVersion(version);
        dubboEnvironment.setGroup(group);
        return dubboEnvironment;
    }

    private Integer parsePort(String port) {
        if (StringUtils.isBlank(port)) return 20880;
        try {
            int portInt =  Integer.parseInt(port);
            if (portInt >= 10 && portInt <= 65535){
                return portInt;
            }
        }catch (Exception ignore){

        }
        return 20880;
    }

    @Override
    protected JComponent createCenterPanel() {
        return panel;
    }
}
